#include "common/common.h"

void printf(const char* str){
    static  uint16_t* VideoMemory = (uint16_t*)0xb8000;
    static uint8_t x, y;
    for(int i = 0;str[i] != '\0';++i){
        
        //revice a enter char
        switch(str[i]){

            case '\n':
                y++;
                x = 0;
                break;
            default:
                VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00 ) | str[i];
                x++;
                break;
        }

        //over the hight
        if(x >= 80){
            y++;
            x = 0;
        }
        //over the width
        if(y >= 25){
            for(y = 0 ;y < 25; y++){
                for(x = 0;x < 80; x++){
                     VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00 ) | ' ';
                }
            }
            x = 0;
            y = 0;
        }
        
    }
    
}

void printfHex(uint8_t value){
        char* foo = "00";
        char* hex = "0123456789ABCDEF";
        foo[0] = hex[(value >> 4) & 0xF];
        foo[1] = hex[value & 0xF];
        printf(foo);
}

void cleanScreen(){
    static  uint16_t* VideoMemory = (uint16_t*)0xb8000;
    for (int i = 0; i < 80 * 25; i++)
    {
       VideoMemory[i] = (VideoMemory[i] & 0xFF00 ) | ' ';
    }
    
}


void printf32Hex(uint32_t value){
        
       printfHex((value >> 24)  & 0xFF);
       printfHex((value >> 16)  & 0xFF);
       printfHex((value >> 8)  & 0xFF);
       printfHex((value)  & 0xFF);
       
}